{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 创建客户端 \n",
    "- 设置连接选项\n",
    "- 设置回调函数\n",
    "- 建立连接"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import paho.mqtt.client as mqtt\n",
    "\n",
    "\n",
    "# 当客户端收到 CONNACK 消息类型\n",
    "def on_connect(client, userdata, flags, rc):\n",
    "    print(\"Connected with result code \" + str(rc))\n",
    "    # 连接成功后进行订阅主题操作\n",
    "    client.subscribe(\"$SYS/#\")\n",
    "\n",
    "    \n",
    "# 当客户端收到 PUBLISH 消息类型.\n",
    "def on_message(client, userdata, msg):\n",
    "    print(msg.topic + \" \" + str(msg.payload))\n",
    "\n",
    "\n",
    "client = mqtt.Client()\n",
    "client.on_connect = on_connect\n",
    "client.on_message = on_message\n",
    "\n",
    "client.connect(\"60.205.202.24\", 31883, 60)\n",
    "\n",
    "# Blocking call that processes network traffic, dispatches callbacks and\n",
    "# handles reconnecting.\n",
    "# Other loop*() functions are available that give a threaded interface and a\n",
    "# manual interface.\n",
    "client.loop_forever()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Client 常用方法\n",
    "\n",
    "- 创建客户端实例\n",
    "- 连接到服务器： connect\\*() 方法\n",
    "- 与服务器保持网络连接：loop\\*() 方法\n",
    "- 使用 subscribe() 方法订阅与接受消息\n",
    "- 使用 publish() 方法向服务器发送消息\n",
    "- 使用 disconnect() 与服务器断开连接\n",
    "\n",
    "# Client\n",
    "\n",
    "## 构造器\n",
    "\n",
    "```python\n",
    "Client(client_id=\"\", clean_session=True, userdata=None, protocol=MQTTv311, transport=\"tcp\")\n",
    "\n",
    "```\n",
    "\n",
    "### 参数\n",
    "\n",
    "- client_id\n",
    "\n",
    "连接到代理时使用的唯一客户端 ID 字符串。如果 client_id 是空字符串或者为 None，则将随机生成一个。在这种情况下，clean_session 参数必须是 True。\n",
    "\n",
    "- clean_session\n",
    "\n",
    "一个确定客户端类型的布尔值。如果 True，在该客户端断开连接时服务器将删除有关此客户端的所有信息。如果 False，客户端是持久客户端，即使客户端断开连接，服务器也保留其订阅信息和排队消息。\n",
    "\n",
    "请注意，客户端永远不会在断开连接时丢弃自己的传出消息。调用 connect（）或 reconnect（）将导致重新发送消息。使用 reinitialise（）将客户端重置为其原始状态。\n",
    "\n",
    "- userdata\n",
    "\n",
    "用户定义的任何类型的数据，作为 userdata 参数传递给回调。它可以在稍后用该 user_data_set()功能更新 。\n",
    "\n",
    "- protocol\n",
    "\n",
    "用于此客户端的 MQTT 协议版本。可以是 MQTTv31 或 MQTTv311\n",
    "\n",
    "- transport\n",
    "  设置为“websockets”以通过 WebSockets 发送 MQTT。用默认值“tcp”以使用原始 TCP。\n",
    "\n",
    "示例：\n",
    "\n",
    "```python\n",
    "import paho.mqtt.client as mqtt\n",
    "\n",
    "mqttc = mqtt.Client()\n",
    "```\n",
    "\n",
    "## 常用方法\n",
    "\n",
    "### reinitialise()\n",
    "\n",
    "```python\n",
    "reinitialise(client_id=\"\", clean_session=True, userdata=None)\n",
    "```\n",
    "\n",
    "该 reinitialise()函数将客户端重置为其初始状态，就像刚刚创建它一样。**它采用与 Client()构造函数相同的参数。**\n",
    "\n",
    "## Option functions 可选方法\n",
    "\n",
    "这些函数可以**修改**客户端的执行行为。在大多数情况下，**此操作必须在连接到服务器之前完成。**\n",
    "\n",
    "### max_inflight_messages_set()\n",
    "\n",
    "```python\n",
    "max_inflight_messages_set(self，inflight)\n",
    "```\n",
    "\n",
    "设置 QoS> 0 一次性通过其网络流量的最大消息数。默认为 20.增加此值将消耗更多内存，但可以增加吞吐量。\n",
    "\n",
    "### max_queued_messages_set()\n",
    "\n",
    "```python\n",
    "max_queued_messages_set(self，queue_size)\n",
    "```\n",
    "\n",
    "设置 QoS> 0 的在传出消息队列中挂起的最大数量。默认为 0. 表示无限制。当队列已满时，将丢弃任何后续传出的消息。\n",
    "\n",
    "### message_retry_set()\n",
    "\n",
    "```python\n",
    "message_retry_set(retry)\n",
    "```\n",
    "\n",
    "如果代理没有响应，则设置 QoS> 0 消息的重试间隔（以秒为单位）。默认设置为 5 秒，通常不需要更改。\n",
    "\n",
    "### ws_set_options()\n",
    "\n",
    "```python\n",
    "ws_set_options(self, path='/mqtt'，headers=None)\n",
    "```\n",
    "\n",
    "设置 websocket 连接选项。只有在 transport=\"websockets\"传递给 Client()构造函数时才会使用这些选项。\n",
    "\n",
    "- path\n",
    "\n",
    "  > 要在服务器上使用的 mqtt 路径。\n",
    "\n",
    "- headers\n",
    "  > 指定应附加到标准 websocket 标头的额外标头列表的字典。\n",
    "  > 或者是可调用的函数，传入正常 websocket 标头并返回一个字典，包含连接到服务器的的一系列 header。\n",
    "\n",
    "必须先调用 connect\\*()。在 examples 文件夹下有配合 AWS IoT 平台使用的 Demo。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
